package com.shundr.order.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.common.util.DateUtil;
import com.shundr.frame.util.StringUtil;
import com.shundr.order.model.CargoOrderInfo;

/**
 * 
 * OrderItemAdapter 订单列表适配器
 * 
 * @author larry 2015-4-13 下午4:26:47 
 * @version 1.0.0
 *
 */
public class OrderItemAdapter extends BaseAdapter{
	public List<CargoOrderInfo> mList;
	private Context mContext;

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public OrderItemAdapter(List<CargoOrderInfo> mlist, Context mContext) {
		super();
		this.mList = mlist;
		this.mContext = mContext;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_info_1,null);
			viewHold = new ViewHold();
			viewHold.tvFrom = (TextView) convertView.findViewById(R.id.tv_from);
			viewHold.tvTo = (TextView) convertView.findViewById(R.id.tv_to);
			viewHold.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			viewHold.tvCarCode = (TextView) convertView.findViewById(R.id.tv_car_code);
			viewHold.tvCargoType = (TextView) convertView.findViewById(R.id.tv_cargo_type);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.tvStatus = (TextView) convertView.findViewById(R.id.tv_status);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			CargoOrderInfo info = mList.get(position);
			if (StringUtil.isNull(info.getSrcPlace())) {
				viewHold.tvFrom.setText("起点未知");
			} else {
				viewHold.tvFrom.setText(info.getSrcPlace());
			}
			if (StringUtil.isNull(info.getDstPlace())) {
				viewHold.tvTo.setText("终点未知");
			} else {
				viewHold.tvTo.setText(info.getDstPlace());
			}
			if (!StringUtil.isNull(info.getCargoContactPerson())) {
				viewHold.tvName.setText(info.getCargoContactPerson());
			}else{
				viewHold.tvName.setText("");
			}
			
			if (!StringUtil.isNull(info.getTruckerPlateNumber())) {
				viewHold.tvCarCode.setText(info.getTruckerPlateNumber());
			}else{
				viewHold.tvCarCode.setText("");
			}
			viewHold.tvCarCode.setVisibility(View.GONE);
			
			String cargoName = info.getCargoName();
			if (StringUtil.isNull(cargoName)) {
				viewHold.tvCargoType.setText("货种未知");
			} else {
				viewHold.tvCargoType.setText(cargoName);
			}
			viewHold.tvTime.setText(DateUtil.getInterval(info.getOrderTime()));
			
			if (info.getOrderCargoStatus() == 1 && info.getOrderTruckStatus() == 1) {
				viewHold.tvStatus.setText("已完成");
			} else if (info.getOrderCargoStatus() == 1 && info.getOrderTruckStatus() == 0) {
				viewHold.tvStatus.setText("待处理");
			} else if (info.getOrderCargoStatus() == 0 && info.getOrderTruckStatus() == 1) {
				viewHold.tvStatus.setText("待确认");
			} else {
				viewHold.tvStatus.setText("无效");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHold{
		TextView tvFrom;//起始地
		TextView tvTo;//目的地
		TextView tvName;//车主姓名
		TextView tvCarCode;//车主姓名
		TextView tvCargoType;//货源类型
		TextView tvTime;//时间
		TextView tvStatus;//状态
	}
}
